package itsm.isperp.module.controller.configuration;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.configuration.ConfCi;
import itsm.isperp.module.entity.configuration.ConfCiUsed;
import itsm.isperp.module.service.configuration.ConfCiService;
import itsm.isperp.module.service.configuration.ConfCiUsedService;
import itsm.isperp.module.service.project.ProjProjectService;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 资产使用记录表控制器
 * 
 * @author lizx
 * @date 2014-10-18 18:10
 */
@Controller
@RequestMapping("configuration/ciUsed")
public class ConfCiUsedController extends BaseController<ConfCiUsed> {

	@Autowired
	protected ConfCiUsedService confCiUsedService;
	@Autowired
	protected ConfCiService confCiService;

	@Override
	public ConfCiUsedService getService() {
		return this.confCiUsedService;
	}

	@Override
	public String getPageTitle() {
		return "资产使用记录表";
	}

	@RequestMapping("saveWithCi")
	@ResponseBody
	public ResultMessage saveWithCi(String ciId, String relationId,
			HttpServletRequest req) {
		ConfCi ci = this.confCiService.findOne(ciId);

		ConfCiUsed model = new ConfCiUsed();
		model.setCiId(ci.getId());
		model.setCiAddress(ci.getAddress());
		model.setCiCategoryName(ci.getConfCategoryName());
		model.setCiCode(ci.getCode());
		model.setCiCustomerName(ci.getCusCustomerName());
		model.setCiDepartmentName(ci.getDepartmentName());
	 
		model.setCiName(ci.getName()); 
		model.setCiUseStaffName(ci.getUseStaffName());

		model.setRelationId(relationId);
		this.getService().saveOrUpdate(model);

		return ResultMessage.success();
	}

	@RequestMapping("listCi")
	@ResponseBody
	public PageResponse<ConfCi> listCi(JqGridRequest request,
			HttpServletRequest req) {
		String relationId = req.getParameter("relationId");

		String projectId = req.getParameter("projectId");

		ProjProjectService projProjectService = ContextHolder
				.getSpringBean("projProjectService");
		Map<String, Object> map = projProjectService.findOneToMap(projectId,
				"cus_customer_id");

		request.addFilterRule("cus_customer_id", JqGridFilterRuleOp.eq, map
				.get("cus_customer_id").toString());

		request.setSearchString("  id NOT IN (SELECT ci_id FROM conf_ci_used WHERE relation_id='"
				+ relationId + "')");

		return this.confCiService.findAll(request).toPage();

	}

	@Override
	@RequestMapping("list")
	@ResponseBody
	public PageResponse<ConfCiUsed> list(JqGridRequest request,
			HttpServletRequest req) {

		String relationId = req.getParameter("relationId");
		if (StringUtils.isNotEmpty(relationId)) {
			request.addFilterRule("relationId", JqGridFilterRuleOp.eq,
					relationId);
		}

		return this.getService().findAll(request).toPage();

	}

}
